package com.dks.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.dks.businessUtil.QsMailUtil;
import com.dks.dto.FeedbackSheetDetailDto;
import com.dks.dto.FeedbackSheetDto;
import com.dks.dto.QsMailJsonDto;
import com.dks.mapper.FeedbackSheetMapper;
import com.dks.mapper.QsMailMapper;
import com.dks.models.FeedbackSheet;
import com.dks.models.FeedbackSheetTemplate;
import com.dks.models.QsMail;
import com.dks.repository.IFeedbackSheetRepository;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;
import com.dks.util.PageInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class FeedbackSheetRepository implements IFeedbackSheetRepository {

    private FeedbackSheetMapper feedbackSheetMapper;

    private QsMailMapper qsMailMapper;

    @Autowired
    public FeedbackSheetRepository(final FeedbackSheetMapper feedbackSheetMapperIn,
                                   final QsMailMapper qsMailMapper) {
        this.feedbackSheetMapper = feedbackSheetMapperIn;
        this.qsMailMapper = qsMailMapper;
    }

    @Override
    public Integer insert(FeedbackSheet feedbackSheet) {
        return feedbackSheetMapper.insert(feedbackSheet);
    }

    @Override
    public Integer update(FeedbackSheet feedbackSheet) {
        return feedbackSheetMapper.update(feedbackSheet);
    }

    @Override
    public Integer delete(Integer id) {
        return feedbackSheetMapper.delete(id);
    }

    @Override
    public FeedbackSheet select(Integer id) {
        return feedbackSheetMapper.select(id);
    }

    @Override
    public Integer cntByMap(Map<String, Object> map) {
        return feedbackSheetMapper.cntByMap(map);
    }

    @Override
    public List<Integer> getByMap(Map<String, Object> map) {
        return feedbackSheetMapper.getByMap(map);
    }

    @Override
    public List<FeedbackSheet> getByMapList(Map<String, Object> map) {
        return feedbackSheetMapper.getByMapList(map);
    }

    @Override
    public Integer updateByCode(FeedbackSheet feedbackSheet) {
        return feedbackSheetMapper.updateByCode(feedbackSheet);
    }

    @Override
    public FeedbackSheet selectByBfCode(String bfCode) {
        return feedbackSheetMapper.selectByBfCode(bfCode);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取课堂反馈列表
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: classId 班级id
     * @Param: teacherId 老师id
     * @Param: feedbackSheetTemplateId 模板id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getFeedbackSheetList(Integer pageNum, Integer pageSize, Integer classId, Integer teacherId) {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("teacherId", teacherId);
        map.put("recStatus", 0);
        Page p = PageHelper.startPage(pageNum, pageSize);
        List<FeedbackSheetDto> rewardDtoList = feedbackSheetMapper.getFeedbackSheetList(map);
        PageInfo<FeedbackSheetDto> pageInfo = new PageInfo<>(pageNum, pageSize, p.getTotal(), p.getPages(), rewardDtoList);
        return Result.makeResult(ResultInfo.SUCCESS, pageInfo);
    }

    /**
     * @Author: Ah Nan
     * @Description: 获取课堂反馈详情
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: feedbackSheetiD 反馈id
     * @Param: teacherId 老师id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> getFeedbackSheetInfo(Integer feedbackSheetId, Integer studentId) {
        Map<String, Object> map = new HashMap<>();
        map.put("feedbackSheetId", feedbackSheetId);
        map.put("studentId", studentId);
        map.put("recStatus", 0);
        FeedbackSheetDetailDto feedbackSheetDetailDto = feedbackSheetMapper.getFeedbackSheetDetail(map);
        return Result.makeResult(ResultInfo.SUCCESS, feedbackSheetDetailDto);
    }

    /**
     * @Author: Ah Nan
     * @Description: 提交课堂反馈
     * @Date: 9:52 2020/4/23
     * @Modificd By:
     * @Param: feedbackSheetiD 反馈id
     * @return:
     * @throw: 请描述异常信息
     */
    @Override
    public ResultModel<Object> addFeedbackSheet(FeedbackSheet feedbackSheet) {
        feedbackSheet.setCreateUserId(feedbackSheet.getTeacherId());
        feedbackSheet.setModifyUserId(feedbackSheet.getTeacherId());
        int row = 0;
        try {
            row = feedbackSheetMapper.insert(feedbackSheet);
        } catch (Exception e) {
            return Result.makeResult(ResultInfo.ERR_FEEDBACK_001);

        }
        //添加站内信
        QsMailJsonDto qsMailJsonDto = qsMailMapper.selectQsMailJsonDto(feedbackSheet.getId());
        QsMail qsMail = new QsMail(feedbackSheet.getTeacherId(), 2, JSONObject.toJSONString(qsMailJsonDto), "老师已经给您创建了最新的课堂反馈，快去看看吧", feedbackSheet.getId());
        QsMailUtil qsMailUtil = new QsMailUtil();
        qsMailUtil.insertQsMail(qsMail);
        return Result.makeResult(ResultInfo.SUCCESS, row);
    }

    @Override
    public ResultModel<Object> getFeedbackSheetTemplateInfo(Integer feedbackSheetTemplateId) {
        FeedbackSheetTemplate feedbackSheetTemplate = feedbackSheetMapper.getFeedbackSheetTemplateInfo(feedbackSheetTemplateId);
        return Result.makeResult(ResultInfo.SUCCESS, feedbackSheetTemplate);
    }


}
